// 
//  CommandLineParser.cs
//  
//  Author:
//       http://www.sanity-free.org/128/command_line_style_string_parser_in_csharp.html
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:2.0.50727.3053
//
//     对此文件的更改可能会导致不正确的行为，并且如果
//     重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections;

namespace SharpNote.Core.Command
{


	public class CommandLineParser
	{
		CommandLineParser ()
		{
		}
		public static string[] Parse (string str)
		{
			if (str == null || !(str.Length > 0))
				return new string[0];
			int idx = str.Trim ().IndexOf (" ");
			if (idx == -1)
				return new string[] { str };
			int count = str.Length;
			ArrayList list = new ArrayList ();
			while (count > 0) {
				if (str[0] == '"') {
					int temp = str.IndexOf ("\"", 1, str.Length - 1);
					while (str[temp - 1] == '\\') {
						temp = str.IndexOf ("\"", temp + 1, str.Length - temp - 1);
					}
					idx = temp + 1;
				}
				if (str[0] == '\'') {
					int temp = str.IndexOf ("'", 1, str.Length - 1);
					while (str[temp - 1] == '\\') {
						temp = str.IndexOf ("'", temp + 1, str.Length - temp - 1);
					}
					idx = temp + 1;
				}
				string s = str.Substring (0, idx);
				int left = count - idx;
				str = str.Substring (idx, left).Trim ();
				list.Add (s.Trim ('"'));
				count = str.Length;
				idx = str.IndexOf (" ");
				if (idx == -1) {
					string @add = str.Trim ('"', ' ');
					if (@add.Length > 0) {
						list.Add (@add);
					}
					break;
				}
			}
			return (string[])list.ToArray (typeof(string));
		}
	}
}
